<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="代理名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="代理名称"></el-input>
      </el-form-item>
      <el-form-item label="微信ID" prop="addWxid">
        <el-input v-model="dataForm.addWxid" placeholder="微信ID"></el-input>
      </el-form-item>
      <el-form-item label="微信昵称" prop="wxghName">
        <el-input v-model="dataForm.wxghName" placeholder="微信昵称"></el-input>
      </el-form-item>
      <el-form-item label="招呼语" prop="addText">
        <el-input v-model="dataForm.addText" placeholder="招呼语"></el-input>
      </el-form-item>
      <el-form-item label="公众号ID" prop="addGhid">
        <el-input v-model="dataForm.addGhid" placeholder="公众号ID"></el-input>
      </el-form-item>
      <el-form-item label="公众号昵称" prop="wxghName">
        <el-input v-model="dataForm.wxghName" placeholder="公众号ID"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status" size="small">
          <el-radio :label=0>启用</el-radio>
          <el-radio :label=1>禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          addWxid: '',
          addText: '',
          wxghName: '',
          wxName: '',
          addGhid: '',
          status: 0
        },
        dataRule: {
          name: [{
            required: true,
            message: '代理名称不能为空',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '状态 0启用1禁用不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cards/amgcdkagent/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.amgcdkagent.name
                this.dataForm.addWxid = data.amgcdkagent.addWxid
                this.dataForm.addText = data.amgcdkagent.addText
                this.dataForm.addGhid = data.amgcdkagent.addGhid
                this.dataForm.status = data.amgcdkagent.status
                this.dataForm.wxghName = data.amgcdkagent.wxghName
                this.dataForm.wxName = data.amgcdkagent.wxName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cards/amgcdkagent/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'addWxid': this.dataForm.addWxid,
                'addText': this.dataForm.addText,
                'addGhid': this.dataForm.addGhid,
                'status': this.dataForm.status,
                'wxName': this.dataForm.wxName,
                'wxghName': this.dataForm.wxghName

              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }

</script>
